package com.rentcars.controller;

import com.rentcars.entity.Resource;
import com.rentcars.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 权限管理--系统资源管理
 *
 * @author 肖克
 * @date 2021/3/2 16:43
 */
@RestController
@RequestMapping("/resources")
public class ResourceController {

  private final ResourceService resourceService;

  public ResourceController(ResourceService resourceService) {
    this.resourceService = resourceService;
  }

  @GetMapping("/toResourcesAdd")
  public ModelAndView toResourcesAdd() {
    return new ModelAndView("resources-add");
  }

  @GetMapping("/toResourcesEdit")
  public ModelAndView toResourcesEdit(String permisid) {
    Resource resource = resourceService.getResourcesById(permisid);
    ModelAndView md = new ModelAndView();
    md.addObject("resource", resource);
    md.setViewName("resources-edit");
    return md;
  }

  @GetMapping("/toResourcesTree")
  public ModelAndView toResourcesTree() {
    return new ModelAndView("resources-tree");
  }

  @GetMapping("/toResourcesSelect")
  public ModelAndView toResourcesSelect(
      @RequestParam(required = false, defaultValue = "999") String permisid) {
    List<Resource> resList = resourceService.getResourcesByParentId(permisid);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("resList", resList);
    return new ModelAndView("resources-select", map);
  }

  @GetMapping("/getResources")
  public List<Map<String, String>> getResources(
      @RequestParam(required = false, defaultValue = "1") String isabled) {
    List<Resource> resources = resourceService.getResources(Integer.valueOf(isabled));
    List<Map<String, String>> resourcesList = new ArrayList<>();
    for (Resource resource : resources) {
      Map<String, String> map = new HashMap<String, String>();
      map.put("id", resource.getPermisid());
      map.put("pId", resource.getParentid());
      map.put("name", resource.getPermisname());
      resourcesList.add(map);
    }
    return resourcesList;
  }

  @RequestMapping(value = "")
  public ModelAndView toResourceFtl() {
    return new ModelAndView("resources-list");
  }

  @RequestMapping("/addResource")
  public String addResource(@RequestBody Resource resource) {
    return resourceService.addResource(resource);
  }

  @RequestMapping("/updateResource")
  public void updateResource(@RequestBody Resource resource) {
    resourceService.updateResource(resource);
  }

  @RequestMapping("/delResources")
  public void delResources(String checkedList) {
    String[] split = checkedList.split(",");
    resourceService.delResources(split);
  }


}
